import React, { useState } from 'react'

export default function App() {
  // !timer 肯定不能写到函数里面
  let timer = null
  const [count, setCount] = useState(10)
  const handleClick = () => {
    // !再清除，每次清除的都是 null
    clearInterval(timer)
    timer = setInterval(() => {
      setCount((count) => count - 1)
    }, 1000)
  }
  return (
    <div>
      <h3>{count}</h3>
      <button onClick={handleClick}>开启定时器</button>
    </div>
  )
}
